﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SubjectGroupAddViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The SubjectGroup Edit View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// SubjectGroupAddViewModel Implementation
    /// </summary>
    public class SubjectGroupAddViewModel : StudyDefinitionViewModelBase, ISubjectGroupAddViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// A subjectGroup item
        /// </summary>
        private SubjectGroup currentItem;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SubjectGroupAddViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        public SubjectGroupAddViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService)
            : base(regionManager, studiesService)
        {
            /* ... Relies on abstract base class for dependencies. */

            // Setting up command delegates 
            this.AddSubjectGroup = new DelegateCommand(this.OnAddSubjectGroup);

            this.AddSubjectToSubjectGroup = new DelegateCommand<Subject>(this.OnAddSubjectToSubjectGroup);
            this.RemoveSubjectFromSubjectGroup = new DelegateCommand<Subject>(this.OnRemoveSubjectFromSubjectGroup);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the add subjectGroup command.
        /// </summary>
        public ICommand AddSubjectGroup { get; set; }

        /// <summary>
        /// Gets or sets AddSubjectToSubjectGroup.
        /// </summary>
        public ICommand AddSubjectToSubjectGroup { get; set; }

        /// <summary>
        /// Gets or sets CurrentItem.
        /// </summary>
        public SubjectGroup CurrentItem
        {
            get
            {
                return this.currentItem;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this.currentItem = value;
                this.RaiseOnPropertyChanged("CurrentItem");
            }
        }

        /// <summary>
        /// Gets or sets the mock subjects.
        /// </summary>
        /// <value>
        /// The mock subjects.
        /// </value>
        public ObservableCollection<Subject> MockSubjects { get; set; }

        /// <summary>
        /// Gets or sets RemoveSubjectFromSubjectGroup.
        /// </summary>
        public ICommand RemoveSubjectFromSubjectGroup { get; set; }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>
        /// The subjectGroup add view.
        /// </value>
        public ISubjectGroupAddView View { get; set; }

        #endregion

        #region Implemented Interfaces

        #region ISubjectGroupAddViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // Populate the current item to for use by the view.
            this.currentItem = new SubjectGroup();
            this.currentItem.Subjects = new ObservableCollection<Subject>();

            // THIS IS PURELY FOR DEMONSTRATION PURPOSES.
            this.CreateMockSubjectsForView();
        }

        /// <summary>
        /// Called when [add subjectGroup].
        /// </summary>
        public void OnAddSubjectGroup()
        {
            this.StudiesServiceInstance.AddSubjectGroup(this.currentItem);

            this.CurrentItem = new SubjectGroup();
            this.RaiseOnPropertyChanged("CurrentItem");
        }

        /// <summary>
        /// The on add subject to subject group.
        /// </summary>
        /// <param name="subject">
        /// The subject.
        /// </param>
        public void OnAddSubjectToSubjectGroup(Subject subject)
        {
            if (subject == null)
            {
                return;
            }

            this.CurrentItem.Subjects.Add(subject);
            this.RaiseOnPropertyChanged("CurrentItem");

            
            this.MockSubjects.Remove(subject);
            this.RaiseOnPropertyChanged("MockSubjects");
        }

        /// <summary>
        /// The on remove subject from subject group.
        /// </summary>
        /// <param name="subject">
        /// The subject.
        /// </param>
        public void OnRemoveSubjectFromSubjectGroup(Subject subject)
        {
            if (subject == null)
            {
                return;
            }

            this.CurrentItem.Subjects.Remove(subject);
            this.RaiseOnPropertyChanged("CurrentItem");

            this.MockSubjects.Add(subject);
            this.RaiseOnPropertyChanged("MockSubjects");
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        /// <summary>
        /// Creates the mock subjects for view.
        /// </summary>
        private void CreateMockSubjectsForView()
        {
            this.MockSubjects = new ObservableCollection<Subject>();
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 1111 }); 
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 2222 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 3333 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 4444 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 5555 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 6666 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 7777 });
            this.MockSubjects.Add(new Subject { IsPrimary = true, SpecialIdentification = "Subject : " + 8888 });
        }

        #endregion
    }
}
